var app = getApp();
const myaudio = wx.createInnerAudioContext(); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
      status:1,
      orderdetail:'',
      sjplay:[],
      yhplay:[],
      on_demand:'',
      yhplay2:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var demand_id = options.id;
      this.getorder(demand_id);
      this.geton();
  },

  // 获取订单详情
  getorder:function(id){
      var that = this;
      var param = {
        url:"api/user/demandDetail",
        data:{
          demand_id: id, 
        },
        success:function(e){
          console.log(e)
          var orderdetail = e.data.data;
          var dlen = e.data.data.demand.voice.length;
          var yhplay = that.data.yhplay;
          var sjplay = that.data.sjplay;
          var yhplay2 = that.data.yhplay2;
          
          if(dlen>0){
            for (var i = 0; i < dlen; i++) {
              yhplay[i] = 0;
            }
          }
          if (e.data.data.grab_order.voice){
            var slen = e.data.data.grab_order.voice.length;
            for (var i = 0; i < slen; i++) {
              sjplay[i] = 0;
            }
          }
          if (e.data.data.demand_add) {
            if (e.data.data.demand_add.voice){
              for (var i = 0; i < e.data.data.demand_add.voice.length; i++) {
                yhplay2[i] = 0;
              }
            }
          }
          that.setData({
            orderdetail:e.data.data,
            yhplay:yhplay,
            sjplay:sjplay,
            yhplay2:yhplay2
          })  
        }
        
      };
      app.post(param);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  makecall:function(e){
    var num = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: num
    })
  },
  // 预览图片
  seeimg: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(this.data.orderdetail)
    wx.previewImage({
      current: this.data.orderdetail.demand.imgs[index], // 当前显示图片的http链接  
      urls: this.data.orderdetail.demand.imgs // 需要预览的图片http链接列表  
    })
  },

  sjvoice:function(e){
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var sjplay = this.data.sjplay;
    var yhplay = this.data.yhplay;
    var yhplay2 = this.data.yhplay2;
    for (var i = 0; i < yhplay2.length; i++) {
      yhplay2[i] = 0
    }
    for(var i=0;i<sjplay.length;i++){
      sjplay[i]=0
    }
    for (var i = 0; i < yhplay.length; i++) {
      yhplay[i] = 0
    }
    myaudio.src = src;
      if(sjplay[index] == 0){
        myaudio.play();
        sjplay[index]=1;
        this.setData({
          sjplay:sjplay
        })
        myaudio.onEnded(() => {
          sjplay[index]=0;
          this.setData({
            sjplay: sjplay
          })
        })
      }else{
        myaudio.pause();
        sjplay[index]=0;
        this.setData({
          sjplay: sjplay
        })
      }
  },
  yhvoice: function (e) {
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var yhplay = this.data.yhplay;
    var sjplay = this.data.sjplay;
    var yhplay2 = this.data.yhplay2;
    for (var i = 0; i < sjplay.length; i++) {
      sjplay[i] = 0
    }
    for (var i = 0; i < yhplay.length; i++) {
      yhplay[i] = 0
    }
    for (var i = 0; i < yhplay2.length; i++) {
      yhplay2[i] = 0
    }
    myaudio.src = src;
    if (yhplay[index] == 0) {
      myaudio.play();
      yhplay[index]=1;
      this.setData({
        yhplay: yhplay
      })

      myaudio.onEnded(() => {
        yhplay[index]=0;
        this.setData({
          yhplay: yhplay
        })
      })

    } else {
      myaudio.pause();
      yhplay[index]=0;
      this.setData({
        yhplay:yhplay
      })
    }
  },
  yhvoice2: function (e) {
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var yhplay = this.data.yhplay;
    var sjplay = this.data.sjplay;
    var yhplay2 = this.data.yhplay2;
    for (var i = 0; i < sjplay.length; i++) {
      sjplay[i] = 0
    }
    for (var i = 0; i < yhplay.length; i++) {
      yhplay[i] = 0
    }
    for (var i = 0; i < yhplay2.length; i++) {
      yhplay2[i] = 0
    }
    myaudio.src = src;
    if (yhplay2[index] == 0) {
      myaudio.play();
      yhplay2[index] = 1;
      this.setData({
        yhplay2: yhplay2
      })

      myaudio.onEnded(() => {
        yhplay[index] = 0;
        this.setData({
          yhplay2: yhplay2
        })
      })

    } else {
      myaudio.pause();
      yhplay[index] = 0;
      this.setData({
        yhplay2: yhplay2
      })
    }
  },
  payit: function () {
    var userid = wx.getStorageSync('userId');
    var id = this.data.orderdetail.grab_order.id;
    var that = this;
    var param = {
      url: "api/index/payOrder",
      data: {
        user_id: userid,
        grab_orders_id: id
      },
      success: function (e) {
        if (e.data.code == 1) {
          var nonceStr = e.data.data.pay_data.nonceStr;
          var packagenum = e.data.data.pay_data.package;
          var paySign = e.data.data.pay_data.paySign;
          var signType = e.data.data.pay_data.signType;
          var timeStamp = e.data.data.pay_data.timeStamp;
          var order_id = e.data.data.order_id;
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            package: packagenum,
            signType: signType,
            paySign: paySign,
            success(res) {
              that.successpay(order_id)
            },
            fail(res) {

            }
          })
        }
      }
    };
    app.post(param);
  },

  // 支付成功
  successpay: function (id) {
    var param = {
      url: "api/index/paySuccess",
      data: {
        order_id: id
      },
      success: function (e) {
        wx.showToast({
          title: e.data.msg,
          icon: "none",
          duration: 2000
        })
        if (e.data.code == 1) {
          wx.navigateBack({
            delta: 1
          })
        }
      }
    };
    app.post(param);
  },

  daohang:function(){
    var detail = this.data.orderdetail;
    var lat = detail.grab_order.s_latitude;
    var lng = detail.grab_order.s_longitude;
    var shopname = detail.grab_order.shop_name;
    wx.openLocation({
      latitude:parseFloat(lat),
      longitude:parseFloat(lng),
      name:shopname,
    })
  },

  refabu:function(){
    var on_demand = this.data.on_demand;
    var orderdetail = this.data.orderdetail;
    app.globalData.detail = orderdetail;
    console.log(on_demand)
    if(Number(on_demand) == 0){
      wx.switchTab({
        url: '/pages/index/index/index',
      })
    } else if (Number(on_demand) == 1){
      wx.showToast({
        title: '你有进行中的需求，不能重新发布',
        icon:'none',
        duration:2000
      })
    }
  },
  // 获取是否有进行中的需求
  geton: function () {
    var that = this;
    var userid = wx.getStorageSync('userId');
    var on = {
      url: 'api/index/getOnDemand',
      data: {
        user_id: userid
      },
      success: function (e) {
        console.log(e)
        var on_demand = e.data.data.on_demand;
        that.setData({
          on_demand: on_demand
        })
      }
    };
    app.post(on);
  },
})